package com.hoshiicloud.user.controller.usercenter;

import com.hoshiicloud.common.rpc.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * C端用户登录相关操作Controller类
 *
 * @author : yangcheng
 * @date : 2019/5/13 19:16
 * @mail : maiboyc@163.com
 **/
@RestController
@RequestMapping("/usercenter/login")
@Api(value = "登录管理", tags = {"登录相关"}, produces = MediaType.APPLICATION_JSON_VALUE)
public class LoginController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    // 小程序APPID
    private static final String APPID = "wx9ba91205fa359c1b";

    // 小程序SECRET
    private static final String SECRET = "2abe911b34f337cb2a42e1bc6d637394";

    // 临时登录Code
    private String code;

    /**
     * 发送验证码
     * @param mobile 手机号码
     * @return Response
     */
    @GetMapping("/{mobile}")
    @ApiOperation(value = "发送手机验证码", notes = "根据手机号码发送验证码", consumes = MediaType.ALL_VALUE)
    public Response verifCode(@PathVariable("mobile") String mobile) {
        Response rs;
        try {
            rs = new Response();
            // 1. 调用短信服务发送验证码
            // 2. 把手机号码和验证码记录到用户表
        } catch (Exception e) {
            logger.error("根据手机号码发送验证码异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }

    // 手机号+验证码注册

    // 手机号+验证码登录
    @PostMapping("/codeLogin")
    @ApiOperation(value = "使用手机验证码登录", notes = "根据手机号+验证码登录", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParams ({
            @ApiImplicitParam(name = "mobile", value = "手机号码", required = true, dataType = "String", example = "18620195709", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "String", example = "068232", paramType = "query")
    })
    public Response codeLogin(String mobile, String code) {
        Response rs;
        try {
            rs = new Response();
            // 1. 拿着手机号码和验证码去redis或者数据表中对比（这里还没想清楚怎么做）
        } catch (Exception e) {
            logger.error("根据手机号码和验证码登录异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }

    // 手机号+密码登录
    @PostMapping("/passwordLogin")
    @ApiOperation(value = "手机号+密码登录", notes = "根据手机号+密码登录", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParams ({
            @ApiImplicitParam(name = "mobile", value = "手机号码", required = true, dataType = "String", example = "18620195709", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", example = "123456", paramType = "query")
    })
    public Response passwordLogin(String mobile, String password) {
        Response rs;
        try {
            rs = new Response();
            // 1. 拿着手机号码和密码去redis或者数据表中对比（这里还没想清楚怎么做）
        } catch (Exception e) {
            logger.error("根据手机号码和密码登录异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }


    // 忘记密码-验证手机（手机号、短信验证码、 图形验证码）
    @PostMapping("/verifMobile")
    @ApiOperation(value = "忘记密码", notes = "忘记密码-验证手机", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParams ({
            @ApiImplicitParam(name = "mobile", value = "手机号码", required = true, dataType = "String", example = "18620195709", paramType = "query"),
            @ApiImplicitParam(name = "smsCode", value = "短信验证码", required = true, dataType = "String", example = "123456", paramType = "query"),
            @ApiImplicitParam(name = "captcha", value = "图形验证码", required = true, dataType = "String", example = "DF54G2", paramType = "query")
    })
    public Response verifMobile(String mobile, String smsCode, String captcha) {
        Response rs;
        try {
            rs = new Response();
            // 1. 拿着手机号码、短信验证码、图形验证码去比对
        } catch (Exception e) {
            logger.error("验证手机号码出现异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }

    // 邮箱动态码
    @PostMapping("/emailCode")
    @ApiOperation(value = "发送邮箱动态码", notes = "发送邮箱动态码", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParams ({
            @ApiImplicitParam(name = "email", value = "邮箱地址", required = true, dataType = "String", example = "maiboyc@163.com", paramType = "query")
    })
    public Response emailCode(String email) {
        Response rs;
        try {
            rs = new Response();
            // 1. 调用邮件服务发送邮件动态码
        } catch (Exception e) {
            logger.error("根据邮箱地址发送邮件动态码出现异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }

    // 邮箱和动态码登录
    @PostMapping("/loginByEmailCode")
    @ApiOperation(value = "邮箱动态码登录", notes = "使用邮箱和动态码登录", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParams ({
            @ApiImplicitParam(name = "email", value = "邮箱地址", required = true, dataType = "String", example = "maiboyc@163.com", paramType = "query"),
            @ApiImplicitParam(name = "emailCode", value = "邮箱验证码", required = true, dataType = "String", example = "878981", paramType = "query")
    })
    public Response loginByEmailCode(String email, String emailCode) {
        Response rs;
        try {
            rs = new Response();
            // 1. 使用邮箱和动态码登录
        } catch (Exception e) {
            logger.error("根据邮箱和邮箱动态码登录出现异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }

    // 邮箱和密码登录
    @PostMapping("/loginByEmailPassword")
    @ApiOperation(value = "邮箱+密码登录", notes = "使用邮箱和密码登录", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParams ({
            @ApiImplicitParam(name = "email", value = "邮箱地址", required = true, dataType = "String", example = "maiboyc@163.com", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "邮箱地址", required = true, dataType = "String", example = "123456", paramType = "query")
    })
    public Response loginByEmailPassword(String email, String password) {
        Response rs;
        try {
            rs = new Response();
            // 1. 使用邮箱和密码登录
        } catch (Exception e) {
            logger.error("根据邮箱和密码登录出现异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }

    // 微信登录(用户在app端点击微信登录调用微信开放平台授权登录，拿到信息以后需要存入到当前的会员信息表中)
    @PostMapping("/loginByWeChat")
    @ApiOperation(value = "微信登录", notes = "使用微信登录", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParams ({
            @ApiImplicitParam(name = "code", value = "临时登录标识", required = true, dataType = "String", example = "123456", paramType = "query"),
            @ApiImplicitParam(name = "nickname", value = "用户昵称", dataType = "String", example = "123456", paramType = "query"),
            @ApiImplicitParam(name = "sex", value = "性别 1-男 2-女", dataType = "String", example = "123456", paramType = "query"),
            @ApiImplicitParam(name = "province", value = "省份", dataType = "String", example = "123456", paramType = "query"),
            @ApiImplicitParam(name = "city", value = "城市", dataType = "String", example = "123456", paramType = "query"),
            @ApiImplicitParam(name = "country", value = "国家", dataType = "String", example = "123456", paramType = "query"),
            @ApiImplicitParam(name = "headimgurl", value = "用户头像", dataType = "String", example = "123456", paramType = "query"),
            @ApiImplicitParam(name = "privilege", value = "用户特权信息", dataType = "String", example = "123456", paramType = "query")
    })
    public Response loginByWeChat(String code, String nickname, String sex, String province, String city, String country, String headimgurl, String privilege) {
        Response rs;
        try {
            String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+APPID+"&secret="+SECRET+"&js_code="+ code +"&grant_type=authorization_code";
            RestTemplate restTemplate = new RestTemplate();
            //进行网络请求,访问url接口
            ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
            //根据返回值进行后续操作
            if(responseEntity != null && responseEntity.getStatusCode() == HttpStatus.OK) {
                String sessionData = responseEntity.getBody();
                //解析从微信服务器获得的openid和session_key;
                //WeChatSession weChatSession = JSON.parseObject(sessionData,WeChatSession.class);
                //获取用户的唯一标识
                //String openid = weChatSession.getOpenid();
                //获取会话秘钥
                //String session_key = weChatSession.getSession_key();
                //下面就可以写自己的业务代码了
                //最后要返回一个自定义的登录态,用来做后续数据传输的验证
                rs = Response.data(sessionData);
                // 1. 接收APP端微信登录传递过来的参数
            } else {
                rs = Response.failed();
            }
        } catch (Exception e) {
            logger.error("微信登录出现异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }

    // qq登录(用户在app端点击QQ登录调用QQ开放平台授权登录，拿到信息以后需要存入到当前的会员信息表中)
    @PostMapping("/loginByQQ")
    @ApiOperation(value = "QQ登录", notes = "使用QQ登录", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParams ({
            @ApiImplicitParam(name = "email", value = "邮箱地址", required = true, dataType = "String", example = "maiboyc@163.com", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "邮箱地址", required = true, dataType = "String", example = "123456", paramType = "query")
    })
    public Response loginByQQ(String email, String password) {
        Response rs;
        try {
            rs = new Response();
            // 1. 接收APP端QQ登录传递过来的参数
        } catch (Exception e) {
            logger.error("QQ登录出现异常"+"["+e.getMessage()+"]");
            rs = Response.failed();
        }
        return rs;
    }

}